package com.cn.ysf.newbeemall.controller.mall;

import com.alibaba.fastjson.JSONObject;
import com.cn.ysf.newbeemall.common.Constants;
import com.cn.ysf.newbeemall.common.IndexConfigTypeEnum;
import com.cn.ysf.newbeemall.controller.vo.NewBeeMallIndexCarouselVO;
import com.cn.ysf.newbeemall.controller.vo.NewBeeMallIndexCategoryVO;
import com.cn.ysf.newbeemall.controller.vo.NewBeeMallIndexConfigGoodsVO;
import com.cn.ysf.newbeemall.service.NewBeeMallCarouselService;
import com.cn.ysf.newbeemall.service.NewBeeMallCategoryService;
import com.cn.ysf.newbeemall.service.NewBeeMallIndexConfigService;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author ysf
 * @date 2019/12/2 13:33
 */
@Controller
public class IndexController {


    @Resource
    private NewBeeMallIndexConfigService newBeeMallIndexConfigService;

    @Resource
    private NewBeeMallCarouselService newBeeMallCarouselService;

    @Resource
    private NewBeeMallCategoryService newBeeMallCategoryService;

    @GetMapping({"/index", "/", "/index.html"})
    public String IndexPage(HttpServletRequest request){
        //分类数据
        List<NewBeeMallIndexCategoryVO> categories=newBeeMallCategoryService.getCategoriesForIndex();
        //如果获取的分类数据为(null)时返回错误页面
        if(CollectionUtils.isEmpty(categories)){
            return "error/error_5xx";
        }
        //首页轮播图片的数量
        List<NewBeeMallIndexCarouselVO> carouselVOSlist=newBeeMallCarouselService.getCarouselsForIndex(Constants.INDEX_CAROUSEL_NUMBER);
        //热卖商品
        List<NewBeeMallIndexConfigGoodsVO> hotGoodses=newBeeMallIndexConfigService.getConfigGoodsesForIndex(IndexConfigTypeEnum.INDEX_GOODS_HOT.getType(),Constants.INDEX_GOODS_HOT_NUMBER);
        //首页新品
        List<NewBeeMallIndexConfigGoodsVO> newGoodses=newBeeMallIndexConfigService.getConfigGoodsesForIndex(IndexConfigTypeEnum.INDEX_GOODS_NEW.getType(),Constants.INDEX_GOODS_NEW_NUMBER);
        //为您推荐商品
        List<NewBeeMallIndexConfigGoodsVO> recommendGoodses=newBeeMallIndexConfigService.getConfigGoodsesForIndex(IndexConfigTypeEnum.INDEX_GOODS_RECOMMOND.getType(),Constants.INDEX_GOODS_RECOMMOND_NUMBER);
        request.setAttribute("categories",categories);     //分类数据
        request.setAttribute("carousels",carouselVOSlist);   //轮播图
        request.setAttribute("hotGoodses",hotGoodses);   //热销商品

        //System.out.println("hotgoods is "+json);
        request.setAttribute("newGoodses",newGoodses);   //新品
        String json= JSONObject.toJSONString(newGoodses);
        //System.out.println("新品上线的商品是："+json);
        request.setAttribute("recommendGoodses",recommendGoodses);   //为您推荐的商品
        return "mall/index";
    }






}
